package com.ruoyi.cs.base.supplier.service;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cs.base.supplier.domain.SupplierContact;
import com.ruoyi.cs.base.supplier.domain.SupplierInfo;
import com.ruoyi.cs.base.supplier.mapper.SupplierInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 供应商信息
 * 业务层执行
 * @author Drj 2021/6/28
 */
@Service
public class SupplierInfoServiceImpl implements ISupplierInfoService {
    @Autowired
    private SupplierInfoMapper supplierInfoMapper;


    /**
     * 供应商信息查询
     *
     * @param supplierInfo
     * @return
     */
    @Override
    public List<SupplierInfo> selectSupplierInfoList(SupplierInfo supplierInfo) {
        List<SupplierInfo> supplierInfos = supplierInfoMapper.selectSupplierInfoList(supplierInfo);
        for (int i = 0; i < supplierInfos.size(); i++) {
            if (StringUtils.isNotNull(supplierInfoMapper.selectSupplierContactListById(supplierInfos.get(i).getSupplierInfoId()))){
                //存在，则存入供应商信息对应联系人信息
                supplierInfos.get(i).setSupplierContacts(supplierInfoMapper.selectSupplierContactListById(supplierInfos.get(i).getSupplierInfoId()));
            }
        }
        return supplierInfos;
    }

    /**
     * 按编号查询
     *
     * @param supplierInfoId 供应商信息编号
     * @return
     */
    @Override
    public SupplierInfo selectSupplierInfoById(String supplierInfoId) {
        return supplierInfoMapper.selectSupplierInfoById(supplierInfoId);
    }

    /**
     * 供应商信息新增
     *
     * @param supplierInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSupplierInfo(SupplierInfo supplierInfo) throws Exception{
        if (StringUtils.isNotNull(supplierInfoMapper.selectSupplierInfoById(supplierInfo.getSupplierInfoId()))){
            //存在
            return  0;
        }
        //不存在
        supplierInfoMapper.insertSupplierInfo(supplierInfo);//供应商信息据
        List<SupplierContact> supplierContacts = supplierInfo.getSupplierContacts();//供应商信息联系人信息
        for (int i = 0; i < supplierContacts.size(); i++) {
            supplierInfoMapper.insertSupplierContact(supplierContacts.get(i));//供应商信息联系人信息
        }
        return 1;

    }

    /**
     * 供应商信息更新
     *
     * @param supplierInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSupplierInfo(SupplierInfo supplierInfo) throws Exception{
        if (!StringUtils.isNotNull(supplierInfoMapper.selectSupplierInfoById(supplierInfo.getSupplierInfoId()))){
            //不存在
            return  0;
        }
        //存在供应商信息单据
        supplierInfoMapper.updateSupplierInfo(supplierInfo);//供应商信息单据
        List<SupplierContact> supplierContacts1 = supplierInfoMapper.selectSupplierContactListById(supplierInfo.getSupplierInfoId());
        List<SupplierContact> supplierContacts = supplierInfo.getSupplierContacts();//供应商信息联系人信息
        for (int i = 0; i < supplierContacts.size(); i++) {
            SupplierContact supplierContact = supplierInfoMapper.selectSupplierContactById(supplierContacts.get(i).getSupplierContactId());
            if (StringUtils.isNotNull(supplierContact)){
                //存在供应商联系人详细
                for (int j = 0; j < supplierContacts1.size(); j++) {
                    if (supplierContacts1.get(j).getSupplierContactId().equals(supplierContact.getSupplierContactId())){
                        supplierContacts1.remove(j);//移除新旧都有的部分
                        break;
                    }
                }//更新
                supplierInfoMapper.updateSupplierContact(supplierContacts.get(i));
            }else{//新增
                supplierInfoMapper.insertSupplierContact(supplierContacts.get(i));
            }
        }
        for (int i = 0; i < supplierContacts1.size(); i++) {
            //删除新旧重叠以外的部分
            supplierInfoMapper.deleteSupplierContactById(supplierContacts1.get(i).getSupplierContactId());
        }
        return 1;
    }

    /**
     * 更新供应商信息删除标志位
     *
     * @param supplierInfo
     * @return
     */
    @Override
    public int updateSupplierInfoDeleteFlag(SupplierInfo supplierInfo) {
        return supplierInfoMapper.updateSupplierInfoDeleteFlag(supplierInfo);
    }
}
